package model.altarica_fields;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

public class Sync {

    private ArrayList<SyncElement> syncs;

    public class SyncElement {

        private ArrayList<String> subs;
        private Event event;
        private boolean optional;

        public SyncElement(ArrayList<String> subs, boolean optional) {

            this.subs = subs;
            this.event = null;
            this.optional = optional;
        }

        public void setEvent(Event e) {

            this.event = e;
        }

        public ArrayList<String> getSubs() {

            return this.subs;
        }

        @Override
        public String toString() {

            String str = new String();

            for (String id : subs) {

                str += id + ".";
            }

            if (event != null) {
                str += event.getId();
            } else {
                str = str.substring(0, str.length() - 1);
            }

            if (optional) {
                str += "?";
            }

            return str;
        }
    };

    public Sync() {

        this.syncs = new ArrayList<SyncElement>();
    }

    public void setEvent(SyncElement sync, Event e) {

        sync.setEvent(e);
    }

    public void addEventSubs(ArrayList<String> ids, boolean optional) {

        this.syncs.add(new SyncElement(ids, optional));
    }

    public Collection<SyncElement> getSyncs() {
        return Collections.unmodifiableCollection(this.syncs);
    }

    @Override
    public String toString() {

        String str = "< ";

        for (SyncElement sync : syncs) {

            str += sync + ", ";
        }

        str = str.substring(0, str.length() - 2);

        str += " >";

        return str;
    }
}
